package com.youlai.boot.system.controller;

import com.youlai.boot.common.annotation.Log;
import com.youlai.boot.common.enums.LogModuleEnum;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.system.model.Goods;
import com.youlai.boot.system.model.GoodsType;
import com.youlai.boot.system.model.form.RoleForm;
import com.youlai.boot.system.model.query.GoodsPageQuery;
import com.youlai.boot.system.service.GoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制层
 *
 * @author Dodngrj
 */
@Tag(name = "02.用户接口")
@RestController
@RequestMapping("/api/v1/goods")
@RequiredArgsConstructor
public class GoodsController {

    private final GoodsService goodsService;

    @Operation(summary = "用户分页列表")
    @GetMapping("/page")
    public Result<List<Goods>> listPagedUsers(GoodsPageQuery queryParams) {
        List<Goods> goods = goodsService.selectAll();
        return Result.success(goods);
    }

    @GetMapping("/delete")
    @Log(value = "删除", module = LogModuleEnum.USER)
    public Result<List<Goods>> delete(GoodsPageQuery queryParams) {
        List<Goods> goods = goodsService.delete(queryParams.getId());
        return Result.success(goods);
    }

    @Operation(summary = "商品表单数据")
    @GetMapping("/{goodsId}/form")
    public Result<Goods> getRoleForm(@Parameter(description = "商品ID") @PathVariable Long goodsId) {
        Goods goods = goodsService.selectById(goodsId);
        return Result.success(goods);
    }

    @GetMapping("/{goodsTypeId}/formGoodsType")
    public Result<GoodsType> formGoodsType(@PathVariable Integer goodsTypeId) {
        GoodsType goodsType = goodsService.selectGoodsTypeId(goodsTypeId);
        return Result.success(goodsType);
    }

    @GetMapping("/selectGoodsType")
    public Result<List<GoodsType>> selectGoodsType(GoodsType goodsType) {
        List<GoodsType> goodsTypes = goodsService.selectGoodsType(goodsType);
        return Result.success(goodsTypes);
    }


    @Operation(summary = "修改商品")
    @PutMapping(value = "/{id}")
    public Result<Integer> updateGoods(@RequestBody Goods goods) {
        Integer result = goodsService.update(goods);
        return Result.success(result);
    }


    @Operation(summary = "添加商品")
    @PostMapping(value = "/save")
    public Result<Integer> save(@RequestBody Goods goods) {
        Integer result = goodsService.save(goods);
        return Result.success(result);
    }


}
